package com.xn2001.controller;

import com.aliyuncs.exceptions.ClientException;
import com.xn2001.service.SmsService;
import com.xn2001.util.FormUtils;
import com.xn2001.util.RandomUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 乐心湖
 * @date 2020/7/29 23:16
 **/
@RestController
@Slf4j
public class SmsController {
    @Autowired
    private SmsService smsService;

    @GetMapping("/send/{mobile}")
    public String send(@PathVariable String mobile) throws ClientException {
        //校验手机号是否合法
        if(StringUtils.isEmpty(mobile) || !FormUtils.isMobile(mobile)) {
            log.error("请输入正确的手机号码 ");
            return null;
        }
        //生成验证码
        String checkCode = RandomUtils.getFourBitRandom();
        //发送验证码
        smsService.send(mobile, checkCode);
        return "短信验证码发送成功";
    }
}
